using CriWare;
using System.Collections.Generic;

namespace IQIGame.Onigao.Game
{
    public class GameAudioListener : CriAtomListener
    {
        public static GameAudioListener Instance { get; private set; }

        public static void RefreshInstance()
        {
            _listeners.Sort((x1, x2) => x1.priority.CompareTo(x2.priority));
            Instance = _listeners[_listeners.Count - 1];
        }

        static List<GameAudioListener> _listeners = new List<GameAudioListener>();

        // 优先级
        private int _priority = 0;
        public int priority
        {
            get { return _priority; }
        }

        protected override void OnEnable()
        {
            base.OnEnable();
            _listeners.Add(this);
            RefreshInstance();
        }

        protected override void OnDisable()
        {
            base.OnDisable();
            _listeners.Remove(this);
            if (Instance == this)
            {
                Instance = null;
                if (_listeners.Count > 0)
                {
                    Instance = _listeners[_listeners.Count - 1];
                }
            }
        }
    }
}
